/**=========================================================
 * Module: access-login.js
 * Demo for login api
 =========================================================*/

(function() {
    'use strict';

    angular
        .module('app.login',['getdatamodule'])
        .controller('LoginFormController', LoginFormController)

    ;

    LoginFormController.$inject = ['$http', '$state','$scope','getdataservice','cfpLoadingBar'];
    function LoginFormController($http, $state,$scope,getdataservice,cfpLoadingBar) {
        var vm = this;
        $scope.Animation='ng-fadeInRight';
        activate();
        ////////////////
        function activate() {
            getcaptcha();
            $scope.userpassword = "";
            $scope.username = "";
            $scope.captchacode="";
            // bind here all data from the form
            function getcaptcha() {
                $scope.captchaurl=baseurl+'/User/CaptchaImage?id='+new Date().getTime();
            };
            // place the message if something goes wrong
            $scope.authMsg = "";
            $scope.getcaptchacode=function(){getcaptcha();}
            vm.login = function() {
                $scope.authMsg = "";

            if(vm.loginForm.$valid) {

                $scope.disable = true;
                cfpLoadingBar.start();
              var p=getdataservice.all('/User/Auth/?username=' + $scope.username + '&password=' + $scope.userpassword )
                    p.then(function (data) {

                        if (data.ResultCode == 100 || data.ResultCode == 500) {
                            swal('错误!',data.Memo,'error');
                            $scope.authMsg = data.Memo;
                            getcaptcha();
                        }else
                        if (data.ResultCode == 200 && data.Data.User_id>0 ) {
                            $state.go("app.singleview");
                        }
                        $scope.disable = false;

                    },function () {
                        $scope.disable = false;
                        $scope.authMsg = "连接服务器出错，请联系管理员！"
                    });


            }
            else {
              // set as dirty if the user click directly to login so we show the validation messages
              /*jshint -W106*/
                vm.loginForm.userpassword.$dirty = true;
                vm.loginForm.username.$dirty = true;
                vm.loginForm.captchacode.$dirty=true;
            }
          };
        }
    }
})();
